package com.eastcom.providor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.eastcom.common.db.DataSourceFactory;
import com.eastcom.common.util.ObjectInfoManager;
import com.eastcom.providor.communicate.CommunicateServer;
import com.eastcom.providor.servlet.DataOutLoadServlet;
import com.eastcom.providor.servlet.UploadFileServlet;
import com.eastcom.providor.task.ScanSchedulerManager;
import com.info.data.BaseProvider;
import com.info.data.GlobalData;

public class ApplicationStart implements ServletContextListener{
	
	//private static String separator="\\";  //window 
	
	private static String separator=File.separator;     //Linux

	private static InputStream inputStream;
	
	private CommunicateServer communicateServer=null;
	
	private ScanSchedulerManager scanSchedulerManager;
	
	public void contextDestroyed(ServletContextEvent context) {	
		if(communicateServer!=null)
		    communicateServer.interrupt();
		
		try{
			DataSourceFactory.getInstance().destory();
		    ObjectInfoManager.destory();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		if(scanSchedulerManager!=null)
			scanSchedulerManager.shutDown();
	}

	public void contextInitialized(ServletContextEvent context) {
		
		String servicePath = context.getServletContext().getRealPath("WEB-INF/classes/");
		String path = servicePath+separator+"config";
		
		
		String logPath = path + separator + "config.properties";
		String applicationPath="config/applicationContext.xml";
		//path + separator + "applicationContext.xml";
		
		
		System.out.println("servicePath============================"+servicePath);
		System.out.println("path==================================="+path);
		System.out.println("logPath==============================="+logPath);
		
		System.out.println("applicationPath==============================="+applicationPath);
		File propFile = new File(logPath);
		Properties props = new Properties();
		try {
			inputStream=new FileInputStream(propFile);
			props.load(inputStream);
			String log4jPath=path+separator+props.getProperty("Log4j.cfg.path");
			String appenderName=props.getProperty("Log4j.cfg.appenderName");
			
			DataSourceFactory.setFilePath(path+separator+"jdbc.properties");
			//DataSourceFactory.initial();
			BMSetting setting=new BMSetting(log4jPath,appenderName);
			
			BMSetting.host=props.getProperty("NoticeServerHost");
			BMSetting.port=Integer.parseInt(props.getProperty("NoticeServerPort"));
			BMSetting.receivePort=Integer.parseInt(props.getProperty("ReceivePort"));
			BMSetting.dataCollectorHost=props.getProperty("DataCollectorHost");
			BMSetting.dataCollectorPort=Integer.parseInt(props.getProperty("DataCollectorPort"));
			
			System.out.println("BMSetting.host==============================="+BMSetting.host);
			
			CommunicateServer.applicationContextPath=applicationPath;
//			communicateServer=new CommunicateServer().start();
			
			ScanSchedulerManager.applicationContextPath=applicationPath;
//			scanSchedulerManager=ScanSchedulerManager.getInstance();
			UploadFileServlet.applicationContextPath=applicationPath;
			DataOutLoadServlet.applicationContextPath=applicationPath;
			System.out.println("==============="+new Date()+" PmmonitorSh start completed!");
			BMSetting.getBMlog().info("PmmonitorSh start completed!");
		} 
		catch (Exception e) {
			BMSetting.getBMlog().error("ApplicationStart error", e);
			e.printStackTrace();
		}
		if(inputStream!=null)
			try {
				inputStream.close();
			} 
		    catch (IOException e) {
				e.printStackTrace();
			}
		
		    
		initIndicatorConfig(context);
	}
	
	private void initIndicatorConfig(ServletContextEvent context) {
//		System.out.println("================================");
		String path=context.getServletContext().getRealPath("config");
		String filePath=context.getServletContext().getRealPath("fileData");
		GlobalData.upFilePath=filePath;
		GlobalData.configPath=path;
		
		Properties props = new Properties();
		InputStream inputFile;
		try {
			inputFile = new FileInputStream(path+File.separator+"jdbc.properties");
			props.load(inputFile);
			BaseProvider.driver=props.get("jdbc.driver")+"";
			BaseProvider.url=props.get("jdbc.url")+"";
			BaseProvider.user=props.get("jdbc.username")+"";
			BaseProvider.password=props.get("jdbc.password")+"";
			
			System.out.println(props.get("jdbc.driver"));
			System.out.println(props.get("jdbc.url"));
			System.out.println(props.get("jdbc.username"));
			System.out.println(props.get("jdbc.password"));
			inputFile.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}



		//new ExcelBiz().init();
		
		
//		System.out.println(" path:"+path);
	}

}
